/*
 * @Author: askMeWhy
 * @Date:   2017-12-29 14:44:52
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-02-03 15:10:59
 */
import {
    UPDATE_SELECTED_CONTROL,
    AJAX_REQUEST,
    AJAX_REQUEST_COMPLETE,
    AJAX_LOADING_TEXT,
    // USER_LOGIN,
    USER_LOGIN_SUCCESS,
    // USER_LOGOUT,
    USER_LOGOUT_SUCCESS,
    // USER_GETINFO,
    USER_GETINFO_SUCCESS,
    // USER_REFRESH_TOKEN,
    USER_REFRESH_TOKEN_SUCCESS,
    // DYNAMIC_MENU_ROUTE,
    DYNAMIC_MENU_ROUTE_SUCCESS,
    // AUTH_AREA,
    AUTH_AREA_SUCCESS,
    AUTH_AREA_LOGOUT_SUCCESS,
    AUTH_HISTORY_UPDATE,
    AUTH_HISTORY_REMOVE,
    COLLECTLIST
} from "./mutations-types";

import { localStorage } from "@/utils";
import { AllRoutersMap, AsyncRoutersMap } from "@/router";
import getMenuList from "@core/public/menu-list/get-menu-list";

export const otherMutations = {
    [UPDATE_SELECTED_CONTROL](state: any, value: any) {
        state.selectedControl = value;
    }
};

export const ajaxMutations = {
    [AJAX_REQUEST](state: any) {
        if (state.ajaxLoaderIndex === 0) state.ajaxLoader = true;
        state.ajaxLoaderIndex++;
    },
    [AJAX_REQUEST_COMPLETE](state: any) {
        state.ajaxLoaderIndex--;
        if (state.ajaxLoaderIndex === 0) state.ajaxLoader = false;
    }
};

export const aMapMutations = {
    initAMap(state: any, payload: any) {
        state.aMap = payload;
    }
};

export const ajaxLoadIngText = {
    [AJAX_LOADING_TEXT](state: any, payload: any) {
        state.ajaxLoaderText = payload;
    }
};

export const collectData = {
    [COLLECTLIST](state: any, payload: any) {
        state.collectList = payload;
    }
};

export const userInfoMutations = {
    [USER_LOGIN_SUCCESS](state: any, payload: any) {
        state.user.auth = payload;
        if (payload) {
            state.user.isSignIn = true;
        }
        localStorage.setItem("auth", state.user.auth);
    },
    [USER_GETINFO_SUCCESS](state: any, payload: any) {
        state.user.userInfo = payload;
        localStorage.setItem("userInfo", state.user.userInfo);
    },
    [USER_REFRESH_TOKEN_SUCCESS](state: any, payload: any) {
        state.user.auth = payload;
        localStorage.setItem("auth", payload);
    },
    [USER_LOGOUT_SUCCESS](state: any) {
        const userInfo = localStorage.getItem("userInfo");
        userInfo && localStorage.removeItem("userInfo");
        const auth = localStorage.getItem("auth");
        auth && localStorage.removeItem("auth");
        state.user.isSignIn = false;
        state.dynamicRouterList = [];
        state.dynamicRouterRequest = false;
        state.tabList = [];
        state.tabActiveName = "";
        state.cityList = [];
    },
    [DYNAMIC_MENU_ROUTE_SUCCESS](state: any, payload: any) {
        state.dynamicRouterList = payload;
        state.dynamicRouterRequest = true;
    }
};

export const authAreaMutations = {
    [AUTH_AREA_SUCCESS](state: any, payload: any) {
        state.authArea = payload;
        localStorage.setItem("authArea", state.authArea);
    },
    [AUTH_AREA_LOGOUT_SUCCESS](state: any) {
        const authArea = localStorage.getItem("authArea");
        authArea && localStorage.removeItem("authArea");
        state.authArea = null;
    }
};

export const authHistoryMutations = {
    [AUTH_HISTORY_UPDATE](state: any, payload: any) {
        // console.log(payload);
        if (!state.authHistory) {
            state.authHistory = {
                list: []
            };
        }
        state.authHistory.list = payload.list || [];
        localStorage.setItem("authHistory", state.authHistory);
    },
    [AUTH_HISTORY_REMOVE](state: any) {
        const authHistory = localStorage.getItem("authHistory");
        authHistory && localStorage.removeItem("authHistory");
        state.authHistory = null;
    }
};

export const menuListMutations = {
    setMenuList(state: any, payload: any) {
        const menuList = payload;
        let routersMap: Array<any> = AllRoutersMap || [];
        routersMap = routersMap.concat(AsyncRoutersMap || []);
        const list = getMenuList(menuList, routersMap);
        state.menuList = list;

        const allPagePath: any = {};

        // 所有二级页面，作为key值
        const getKey = (routerList: any) => {
            routerList.forEach((item: any) => {
                if (item.children && item.children.length) {
                    getKey(item.children);
                } else {
                    allPagePath["/" + item.path] = [];
                }
            });
        };

        getKey(list);

        // 铺平，链式
        const allRouter: any[] = [];
        list.forEach((item: any) => {
            if (item.children && item.children.length) {
                item.children.forEach((it: any) => {
                    allRouter.push([item, it]);
                });
            } else {
                allRouter.push([item]);
            }
        });

        allRouter.forEach((item: any) => {
            const key = "/" + item[item.length - 1].path;
            if (allPagePath[key]) {
                allPagePath[key] = item;
            }
        });

        state.allPagePath = allPagePath;
    }
};
